package zdar.ui;

import java.util.ArrayList;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;

import zdar.entities.Action2;
import zdar.entities.MeleeAction;
import zdar.entities.RangeAction;
import zdar.processing.CombatProcessor;

/**
 * This class will build the UI to allow the player to select a Unit's abilities during combat.
 * An ActionTable should read the Unit's ability list and organize the actions into a nice format.
 * Ideally the ActionTable should update itself as the unit acquires or loses abilities.
 * The Buttons in the ActionTable will return the Action to... wherever it ends up going.
 * @author danbye50
 *
 */
public class ActionTable {

	private ArrayList<Action2> actionList;
	private ArrayList<MeleeAction> meleeActions;
	private ArrayList<RangeAction> rangeActions;
	//public ArrayList<eh?> nonCombatActions;
	
	private CombatProcessor combatProc;
	
	public Table table;
	private Skin skin;
	
	public ActionTable(ArrayList<Action2> actionList, CombatProcessor combatProc, Skin skin){
		this.actionList = actionList;
		this.skin = skin;
		this.combatProc = combatProc;
		meleeActions = new ArrayList<MeleeAction>();
		rangeActions = new ArrayList<RangeAction>();
		buildTable();
	}
	
	public void refresh(ArrayList<Action2> actionList){
		this.actionList = actionList;
		buildTable();
	}
	
	public Table getTable(){
		return table;
	}
	
	private void buildTable(){		
		// Step 1: sort actions
		System.out.println();
		for (Action2 action : actionList){
			if (action instanceof MeleeAction){
				meleeActions.add((MeleeAction) action);
			}
			else if (action instanceof RangeAction){
				rangeActions.add((RangeAction) action);
			}
			else{
				System.out.println("who's putting untagged Actions in places they shouldn't be?");
			}
		}
		
		// Step 2: build table
		table = new Table();
		table.left().top();
		// May need to set up some scrolling eventually
		// let's try to keep the sizing setup on the fight screen. We should just build the table here, Screen will handle display aspects
		
		// Step 3: set up buttons		
		// Step 3a: melee buttons
		table.add(new Label("Melee Actions", skin)).width(120).height(15);
		table.row();
		
		for (MeleeAction action : meleeActions){
			final MeleeAction action2 = action;
			Button button = new TextButton(action.getName(), skin);
			button.addListener(new ChangeListener(){
				public void changed(ChangeEvent event, Actor actor) {
					// I think we will need some kind of central combat processor
					// so when the button is clicked, it tells the processor to execute that action
					// in the future the player will select a target, then an action. so the target is out of scope for anything here
					// for now we will just do this:
					System.out.println("You " + action2.getName() + "'d your opponent!");
					combatProc.process(action2);					
				}			
			});
			// Add mouseover tool tip w/ ClickListener thingers
			
			table.add(button).width(120).height(30);
		}
		
		// Step 3b: ranged buttons
		table.row();
		table.add(new Label("Ranged Actions", skin)).width(120).height(15);
		table.row();
		
		for (RangeAction action : rangeActions){
			final RangeAction action2 = action;
			Button button = new TextButton(action.getName(), skin);
			button.addListener(new ChangeListener(){
				public void changed(ChangeEvent event, Actor actor) {
					// I think we will need some kind of central combat processor
					// so when the button is clicked, it tells the processor to execute that action
					// in the future the player will select a target, then an action. so the target is out of scope for anything here
					// for now we will just do this:
					System.out.println("You " + action2.getName() + "'d your opponent!");
					combatProc.process(action2);
				}			
			});
			// Add mouseover tool tip w/ ClickListener thingers
			
			table.add(button).width(120).height(30);
		}
		
		// Step 4: send the table to the Fight Screen
	}
	

}
